<template>
	<view class="bg-color-item ptb-16 br-12">
		<view class="plr-16 ptb-12 flex flex-ai-center" v-for="item in ablePay" :key="item.key"
			@click="changePayType(item)">
			<w-icon :name="item.icon" size="24"></w-icon>
			<text class="pl-12 font-color-textPrimary flex-1">{{item.name && handleLang(item.name)}}</text>
			<view class="" v-if="getAuth(item)">
				<view class="" v-if="item.able">
					<w-icon name="checkmark-circle-fill" key='fill'
						v-if="value.pay_type==item.pay_type&&value.pay_way==item.pay_way" size='16' color='#222222'>
					</w-icon>
					<w-icon name="uncheck-circle-icon" key='uncheck' v-else size='16'></w-icon>
				</view>
				<view class="font-size-12" v-else>
					{{handleLang('卖家未支持该支付通道收款')}}
				</view>
			</view>
			<w-button v-else type="colorful" size="mm" shape="circle" :disabled="!userInfo.real_name_authentication"
				@click='toOpen(item)'>{{handleLang('去开通')}}</w-button>
		</view>
		<w-toast btnType="colorful" v-model="servicePop" :title="servicePriceText" @action="actionServicePop"></w-toast>
	</view>
</template>

<script>
	import {
		openHuiFu,
		CreateHuifuAccountOrder,
		openYiBao
	} from '@/api/wallet/index.js'
	import PayUtil from '@/utils/payUtil.js';
	import {
		basePayList
	} from '@/config/index.js'
	import {
		mapActions
	} from 'vuex'
	export default {
		props: {
			value: {
				type: Object,
				default () {
					return {
						pay_type: null,
						pay_way: null
					}
				}
			},
			order: {
				type: Object,
				default () {
					return {
						product: {}
					}
				}
			}
		},
		data() {
			return {
				servicePriceText: '',
				servicePop: false,
				payStatus: {},
				basePayList
			}
		},
		computed: {
			ablePay() {
				return this.basePayList
			},
			product() {
				return this.order.product || {}
			},
			totalPrice() {
				return (this.product.price * this.order.count).toFixed(2)
			},
		},
		model: {
			prop: "value",
			event: 'change'
		},

		mounted() {
			this.getPayType();
		},
		methods: {
			getPayType() {
				if (this.order.order_type == 1) { //如果是一级都可用
					this.basePayList = this.basePayList.map(v => {
						v.able = true
						return v
					})
				} else {
					let ableType = []
					if (this.order.income_type) {
						ableType = this.order.income_type.split(',')
					}
					this.basePayList.map(v => {
						v.able = ableType.includes(v.pay_way)
						return v
					})
				}
				this.setDefault();
			},
			setDefault() {
				let item = this.basePayList.find(v => {
					return this.getAuth(v) && v.able
				})
				let hasDefault = this.basePayList.find(v => {
					return this.getAuth(v) && v.able && v.pay_way == this.appConfig.default_pay_type
				})
				if (item) {
					this.changePayType(item)
				}
				if (hasDefault) { //设置默认
					this.changePayType(hasDefault)
				}
			},
			toOpen(item) {
				if (item.pay_way == "huifu") {
					this.openHuifuWallet()
				} else if (item.pay_way == 'yibao') {
					this.openYibaoWallet()
				}
			},
			openHuifuWallet() {
				uni.showLoading({
					title: "开通提交中"
				})
				openHuiFu().then(res => {
					let {
						price,
						mer_cust_id,
						redirect_url,
						info
					} = res.data;
					if (!mer_cust_id) {
						this.servicePop = true;
						this.servicePriceText = price;
					} else {
						this.toWeb(info.redirect_url)
					}
				})
			},
			openYibaoWallet() {
				uni.showLoading({
					title: "开通提交中"
				})
				openYiBao().then(res => {
					let {
						redirect_url
					} = res.data.info;
					this.toWeb(redirect_url)
				})
			},
			async actionServicePop(res) {
				if (res) {
					uni.showLoading()
					let param = await CreateHuifuAccountOrder();
					new PayUtil({
						pay_type: 4,
						pay_way: 'huifu',
						order_type: 4,
						order_number: param.data.order_sn
					})
				}
			},
			getAuth({
				auth_key,
				pay_way
			}) {
				if (this.order.order_type == 2) {
					return this.userInfo[auth_key]
				} else {
					if (pay_way == 'huifu' && this.totalPrice != 0) {
						return true;
					}
					return this.userInfo[auth_key]
				}
			},
			changePayType(item) {
				if (this.getAuth(item) && item.able) {
					let {
						pay_type,
						pay_way,
						auth_key
					} = item;
					this.$emit('change', {
						pay_type,
						pay_way
					})
				}
			},
			toWeb(url) {
				// #ifdef H5
				window.location.href = url
				// window.open(url)
				// #endif
				// #ifdef APP-PLUS
				plus.runtime.openURL(url)
				// #endif
			}

		}
	}
</script>

<style>
</style>